<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/9/18
 * Time: 16:26
 */

namespace core_class;

use Illuminate\Database\Capsule\Manager as Capsule;
use GuzzleHttp\Client;
use phpQuery;

class Gather
{
    /**
     * 获取楼盘信息
     */
    public function gather_loupan(){
        $url = $this->loupan_base_url.$this->cur_page;
        $file_contents = Cache::cache_file_get_contents($url);
        phpQuery::newDocument($file_contents);
        foreach (pq('table.ygsf_table1 tr') as $index => $tr){
            $this->loupan_hanghao = $index;
            if($index>0) {
                $data = [
                    "col_0" => pq('td:eq(0)>a', $tr)->text(),
                    "col_1" => pq('td:eq(1)>a', $tr)->text(),
                    "col_2" => pq('td:eq(2)', $tr)->text(),
                    "col_3" => pq('td:eq(3)', $tr)->text(),
                    "col_4" => pq('td:eq(4)', $tr)->text(),
                    "col_5" => pq('td:eq(5)', $tr)->text(),
                    "next_url" => 'http://fgj.xa.gov.cn/ygsf/' . pq('td:eq(0)>a', $tr)->attr("href"),
                    'loupan_url' => $url
                ];
                $this->gather_louzhuang($data['next_url'], $data);
            }
        }
        if($this->cur_page < $this->max_page){
            $this->cur_page++;
            $this->gather_loupan();
        }
    }


    /**
     * 获取楼盘 -> 楼幢
     */
    function gather_louzhuang($url, $loupan){
        $file_contents = Cache::cache_file_get_contents($url);
        phpQuery::newDocument($file_contents);
        foreach (pq('div.ygsf_lz>a') as $index => $a){
            $this->zhuang_hanghao = $index;
            $duri = 'http://fgj.xa.gov.cn/ygsf/Lpb.aspx'.pq($a)->attr('href');
            $data = ['name'=>pq($a)->text(), 'next_url'=> $duri];
            $this->gather_louceng($duri, $loupan, $data);
        }
    }

    /**
     * 获取楼盘 -> 楼幢 -> 楼层
     */
    function gather_louceng($url, $loupan, $louzhuang){
        $file_contents = Cache::cache_file_get_contents($url);
        phpQuery::newDocument($file_contents);
        foreach (pq('table.ygsf_table3 tr') as $index => $tr){
            if($index > 0){
                foreach (pq("td", $tr) as $i => $td){
                    foreach (pq("a.ygsf_fwa", $td) as $ai => $fang){

                        $xiangqing_url = 'http://fgj.xa.gov.cn/ygsf/ashx/GetFwxx.ashx?fwbh='.pq($fang)->attr('data-id');
                        $xiangqing = "";
                        $fanghao = "";
                        $xq_yongtu = '';
                        $xq_huxing = '';
                        $xq_jianmian = '';
                        $xq_shimian = '';
                        $xq_fentan = '';
                        try{
                            $xq = $this->gather_fangjian($louzhuang['next_url'], $xiangqing_url);
                            if(strlen($xq) <= 255){
                                $xqArr = explode(',', $xq);
                                $fanghao = $xqArr[0];
                                $xq_yongtu = $xqArr[3];
                                $xq_huxing = $xqArr[4];
                                $xq_jianmian = $xqArr[5];
                                $xq_shimian = $xqArr[6];
                                $xq_fentan = $xqArr[7];
                                $xiangqing = $xq;
                            }
                        }catch (Exception $exception){

                        }
                        $fdata = [
                            'ceng' => pq('input:eq(0)', $td)->attr('value'),
                            'danyuan' => pq('input:eq(1)', $td)->attr('value'),
                            'status' => pq($fang)->attr('class'),
                            'xiangqing_url' => $xiangqing_url,
                            'xiangqing' => $xiangqing,
                            'fanghao' => $fanghao,
                            'xq_yongtu' => $xq_yongtu,
                            'xq_huxing' => $xq_huxing,
                            'xq_jianmian' => $xq_jianmian,
                            'xq_shimian' => $xq_shimian,
                            'xq_fentan' => $xq_fentan,
                        ];
                        $this->fangjian_index = "{$ai}|{$fdata['danyuan']}单元{$fdata['fanghao']}号";
                        $this->insert_data($loupan, $louzhuang, $fdata);
                        $this->toLog();
                    }
                }
            }
        }
    }


    /**
     * 获取楼盘 -> 楼幢 -> 楼层 -> 单元 -> 房间
     */
    function gather_fangjian($page_url, $url){
        $client = new Client();
        $res = $client->request('POST', $url, [
            'headers' => [
                'Referer' => $page_url
            ]
        ]);
        if($res->getStatusCode()==200){
            return $res->getBody();
        }
        return "";
    }

    function insert_data( $loupan, $louzhuang, $fangwu ){
        date_default_timezone_set("Asia/Shanghai");
        try{
            Capsule::table($this->table_name)->insert([
                '预售证号'  => $loupan['col_0'],
                '备案项目名称' => $loupan['col_1'],
                '项目坐落'  => $loupan['col_2'],
                '开发企业'  => $loupan['col_3'],
                '可售楼幢'  => $loupan['col_4'],
                '发证时间'  => $loupan['col_5'],
                '幢号'    => $louzhuang['name'],
                '楼层'    => $fangwu['ceng'],
                '单元'    => $fangwu['danyuan'],
                '房屋号'   => $fangwu['fanghao'],
                '房屋详情'   => $fangwu['xiangqing'],
                '房屋状态'  => $fangwu['status'],
                '创建时间' => date('Y-m-d H:i:s', time()),
                '更新时间' => date('Y-m-d H:i:s', time()),
                '楼盘URL'         => $loupan['loupan_url'],
                '幢号URL'         => $louzhuang['next_url'],
                '详情URL'         => $fangwu['xiangqing_url'],
                '房屋用途' => $fangwu['xq_yongtu'],
                '房屋户型' => $fangwu['xq_huxing'],
                '建筑面积' => $fangwu['xq_jianmian'],
                '套内面积' => $fangwu['xq_shimian'],
                '分摊面积' => $fangwu['xq_fentan'],
            ]);
        }catch (Exception $e){
            var_dump($e);
        }
    }


    private $kaishi_time;
    private $cur_page;
    private $max_page;
    private $loupan_hanghao = 0;
    private $zhuang_hanghao = 0;
    private $loupan_base_url = 'http://fgj.xa.gov.cn/ygsf/index.aspx?page=';
    private $table_name = 'loupan_fangwu_oop';
    private function __construct($qi_page, $max_page)
    {
        $this->cur_page = $qi_page;
        $this->max_page = $max_page;
        $this->kaishi_time = time();
    }

    private function toLog(){
        echo "\n\n开始时间：".date('Ymd H:i:s', $this->kaishi_time)."->".date('Ymd H:i:s', time())." 已用时长：".((time()-$this->kaishi_time)/60).'m';
        echo "\n楼盘页码：{$this->cur_page}/{$this->max_page} 楼盘页行号：{$this->loupan_hanghao} 幢页行号(楼层)：{$this->zhuang_hanghao} 房间位置：{$this->fangjian_index}";
    }

    public static function run($qi_page, $max_page){
        $gather = new self($qi_page, $max_page);
        $gather->gather_loupan();
    }
}